home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP07.ZIP / CHAP07 / DATATRAN / DATAOBJ.CPP next >
C/C++ Source or Header  |  1993-04-23  |  4KB  |  203 lines

  1. /*
  2.  * DATAOBJ.CPP
  3.  * Data Transfer Object for Chapter 7
  4.  *
  5.  * Implementation of the CDataObject for the Data Transfer Component Object.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "dataobj.h"
  18.  
  19. extern HINSTANCE g_hInst;
  20.  
  21. /*
  22.  * CDataObject::CDataObject
  23.  * CDataObject::~CDataObject
  24.  *
  25.  * Parameters (Constructor):
  26.  *  punkOuter       LPUNKNOWN of a controlling unknown, if it exists.
  27.  *  pfnDestroy      LPFNDESTROYED to call when an object is destroyed.
  28.  */
  29.  
  30. CDataObject::CDataObject(LPUNKNOWN punkOuter, LPFNDESTROYED pfnDestroy)
  31.     {
  32.     m_cRef=0;
  33.     m_punkOuter=punkOuter;
  34.     m_pfnDestroy=pfnDestroy;
  35.  
  36.     m_hList=NULL;
  37.  
  38.     //NULL any contained interfaces initially.
  39.     m_pIDataObject=NULL;
  40.  
  41.     return;
  42.     }
  43.  
  44.  
  45. CDataObject::~CDataObject(void)
  46.     {
  47.     if (NULL!=m_pIDataObject)
  48.         delete m_pIDataObject;
  49.  
  50.     Purge();
  51.  
  52.     if (NULL!=m_hList)
  53.         DestroyWindow(m_hList);
  54.  
  55.     return;
  56.     }
  57.  
  58.  
  59.  
  60. /*
  61.  * CDataObject::FInit
  62.  *
  63.  * Purpose:
  64.  *  Performs any intiailization of a CDataObject that's prone to failure
  65.  *  that we also use internally before exposing the object outside.
  66.  *
  67.  * Parameters:
  68.  *  None
  69.  *
  70.  * Return Value:
  71.  *  BOOL            TRUE if the function is successful, FALSE otherwise.
  72.  */
  73.  
  74. BOOL CDataObject::FInit(void)
  75.     {
  76.     LPUNKNOWN       pIUnknown=(LPUNKNOWN)this;
  77.  
  78.     if (NULL!=m_punkOuter)
  79.         pIUnknown=m_punkOuter;
  80.  
  81.     //Allocate contained interfaces.
  82.     m_pIDataObject=new CImpIDataObject(this, pIUnknown);
  83.  
  84.     if (NULL==m_pIDataObject)
  85.         return FALSE;
  86.  
  87.     m_hList=CreateWindow("listbox", "renderings", WS_POPUP | LBS_OWNERDRAWFIXED
  88.         , 0, 0, 100, 100, HWND_DESKTOP, NULL, g_hInst, NULL);
  89.  
  90.     if (NULL==m_hList)
  91.         return FALSE;
  92.  
  93.     return TRUE;
  94.     }
  95.  
  96.  
  97.  
  98. /*
  99.  * CDataObject::Purge
  100.  *
  101.  * Purpose:
  102.  *  Cleans out all entries in our listbox.
  103.  *
  104.  * Parameters:
  105.  *  None
  106.  *
  107.  * Return Value:
  108.  *  None
  109.  */
  110.  
  111. void CDataObject::Purge(void)
  112.     {
  113.     UINT        i, cItems;
  114.     LPRENDERING pRen;
  115.     DWORD       cb;
  116.  
  117.     if (NULL==m_hList)
  118.         return;
  119.  
  120.     cItems=(UINT)SendMessage(m_hList, LB_GETCOUNT, 0, 0L);
  121.  
  122.     for (i=0; i < cItems; i++)
  123.         {
  124.         cb=SendMessage(m_hList, LB_GETTEXT, i, (LPARAM)(LPVOID)&pRen);
  125.  
  126.         if (sizeof(LPRENDERING)==cb)
  127.             {
  128.             /*
  129.              * Release the data completely being sure to reinstate
  130.              * the original pUnkForRelease.
  131.              */
  132.             pRen->stm.pUnkForRelease=pRen->pUnkOrg;
  133.             ReleaseStgMedium(&pRen->stm);
  134.             delete pRen;
  135.             }
  136.         }
  137.  
  138.     SendMessage(m_hList, LB_RESETCONTENT, 0, 0L);
  139.     return;
  140.     }
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. /*
  148.  * CDataObject::QueryInterface
  149.  * CDataObject::AddRef
  150.  * CDataObject::Release
  151.  *
  152.  * Purpose:
  153.  *  IUnknown members for CDataObject object.
  154.  */
  155.  
  156. STDMETHODIMP CDataObject::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  157.     {
  158.     *ppv=NULL;
  159.  
  160.     if (IsEqualIID(riid, IID_IUnknown))
  161.         *ppv=(LPVOID)this;
  162.  
  163.     if (IsEqualIID(riid, IID_IDataObject))
  164.         *ppv=(LPVOID)m_pIDataObject;
  165.  
  166.     //AddRef any interface we'll return.
  167.     if (NULL!=*ppv)
  168.         {
  169.         ((LPUNKNOWN)*ppv)->AddRef();
  170.         return NOERROR;
  171.         }
  172.  
  173.     return ResultFromScode(E_NOINTERFACE);
  174.     }
  175.  
  176.  
  177. STDMETHODIMP_(ULONG) CDataObject::AddRef(void)
  178.     {
  179.     return ++m_cRef;
  180.     }
  181.  
  182.  
  183. STDMETHODIMP_(ULONG) CDataObject::Release(void)
  184.     {
  185.     ULONG       cRefT;
  186.  
  187.     cRefT=--m_cRef;
  188.  
  189.     if (0==m_cRef)
  190.         {
  191.         /*
  192.          * Tell the housing that an object is going away so it can
  193.          * shut down if appropriate.
  194.          */
  195.         if (NULL!=m_pfnDestroy)
  196.             (*m_pfnDestroy)();
  197.  
  198.         delete this;
  199.         }
  200.  
  201.     return cRefT;
  202.     }
  203.